{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "da6f64bc",
   "metadata": {},
   "source": [
    "# SciPy中的优化器\n",
    "优化器是SciPy中定义的一组程序，它可以找到一个函数的最小值，或者找到一个方程的根。\n",
    "\n",
    "# 优化函数\n",
    "从本质上讲，机器学习中的所有算法都不过是一个复杂的方程，需要在给定数据的帮助下将其最小化。\n",
    "\n",
    "# 方程的根\n",
    "NumPy能够找到多项式和线性方程的根，但它不能找到非线性方程的根，比如这个。\n",
    "\n",
    "x + cos(x)\n",
    "\n",
    "为此，你可以使用SciPy的optimze.root函数。\n",
    "\n",
    "这个函数需要两个必要的参数。\n",
    "\n",
    "fun - 一个代表方程的函数。\n",
    "\n",
    "x0 --一个对根的初始猜测。\n",
    "\n",
    "该函数返回一个包含解的信息的对象。\n",
    "\n",
    "实际的解决方案是在返回对象的属性x下给出的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "050f3b9f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-0.73908513]\n"
     ]
    }
   ],
   "source": [
    "from scipy.optimize import root\n",
    "from math import cos\n",
    "\n",
    "def eqn(x):\n",
    "  return x + cos(x)\n",
    "\n",
    "myroot = root(eqn, 0)\n",
    "\n",
    "print(myroot.x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "05dc8e2a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    fjac: array([[-1.]])\n",
      "     fun: array([0.])\n",
      " message: 'The solution converged.'\n",
      "    nfev: 9\n",
      "     qtf: array([-2.66786593e-13])\n",
      "       r: array([-1.67361202])\n",
      "  status: 1\n",
      " success: True\n",
      "       x: array([-0.73908513])\n"
     ]
    }
   ],
   "source": [
    "# 打印关于解决方案的所有信息（而不仅仅是作为根的x）。\n",
    "print(myroot)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02d654db",
   "metadata": {},
   "source": [
    "# 最小化一个函数\n",
    "在这里，一个函数代表一条曲线，曲线有高点和低点。\n",
    "\n",
    "高点被称为最大值。\n",
    "\n",
    "低点被称为最小值。\n",
    "\n",
    "整条曲线上的最高点被称为全局最大值，其余的被称为局部最大值。\n",
    "\n",
    "整条曲线的最低点称为全局最小值，其余的称为局部最小值。\n",
    "\n",
    "# 寻找最小值\n",
    "我们可以使用 scipy.optimize.minim() 函数来最小化函数。\n",
    "\n",
    "minimize()函数接受以下参数。\n",
    "\n",
    "fun - 一个代表方程的函数。\n",
    "\n",
    "x0 --根的初始猜测。\n",
    "\n",
    "method - 要使用的方法的名称。合法值：\n",
    "    'CG'\n",
    "    \n",
    "    'BFGS'\n",
    "    \n",
    "    '牛顿-CG'\n",
    "    \n",
    "    'L-BFGS-B'\n",
    "    \n",
    "    'TNC'\n",
    "    \n",
    "    'COBYLA'\n",
    "    \n",
    "    'SLSQP'\n",
    "\n",
    "callback - 每次优化迭代后调用的函数。\n",
    "\n",
    "options - 一个定义额外参数的字典。\n",
    "\n",
    "{\n",
    "\n",
    "     \"disp\": boolean - 打印详细的描述\n",
    "     \n",
    "     \"gtol\": 数字 - 错误的容忍度\n",
    "  }\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2e10cf21",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      fun: 1.75\n",
      " hess_inv: array([[0.50000001]])\n",
      "      jac: array([0.])\n",
      "  message: 'Optimization terminated successfully.'\n",
      "     nfev: 8\n",
      "      nit: 2\n",
      "     njev: 4\n",
      "   status: 0\n",
      "  success: True\n",
      "        x: array([-0.50000001])\n"
     ]
    }
   ],
   "source": [
    "from scipy.optimize import minimize\n",
    "\n",
    "def eqn(x):\n",
    "  return x**2 + x + 2\n",
    "\n",
    "mymin = minimize(eqn, 0, method='BFGS')\n",
    "\n",
    "print(mymin)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
